package problem82;

//82.删除排序链表中的重复元素2
//https://leetcode.cn/problems/remove-duplicates-from-sorted-list-ii/?envType=study-plan-v2&envId=top-interview-150

class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        ListNode newHead = new ListNode();
        newHead.next = head;
        ListNode cur = head, prev = newHead;
        while(cur != null) {
            ListNode tmp = cur.next;
            while(tmp != null && tmp.val == cur.val) {
                tmp = tmp.next;
            }
            if(tmp != cur.next) {
                prev.next = tmp;
                cur = tmp;
            }else {
                prev = cur;
                cur = cur.next;
            }
        }
        return newHead.next;
    }
}